#include <stdio.h>
#include <conio.h>
#include <string.h>
#include <malloc.h>
#include "errorcodes.h"
#include "commandmap.h"
#include "linkedlist.h"

//! Global command map for the text interface
CommandMap Mapping;


/*!
  Print program welcome message
*/
void PrintWelcome()
{
	printf("Welcome to Lifeo!\n");
	printf("Lifeo intends to be an open source personal organizer that is intuitive, inclusive, innovative, and runs on all major platforms. You can learn more about the project at the repository home page at http://code.google.com/p/lifeo/\n");
	printf(">>Type help for a list of commands.\n");
}
/*!
  Future feature. Create a task
  Mapped function
  /param args An array of string arguments. See ParseCommand
  /return A global error code
*/
int CreateTask(char **args)
{
	printf("CreateTask n/a\n");
	if (args[0] == 0x0)
		printf("no args\n");
	return EC_FUTURE_FEATURE;
}
/*!
  Future feature. Set a reminder for a task
  Mapped function
  /param args An array of string arguments. See ParseCommand
  /return A global error code
*/
int SetReminder(char **args)
{
	printf("SetReminder n/a\n");
	if (args[0] == 0x0)
		printf("no args\n");
	return EC_FUTURE_FEATURE;
}
/*!
  Future feature. Print a description of a task
  Mapped function
  /param args An array of string arguments. See ParseCommand
  /return A global error code
*/
int DescribeTask(char **args)
{
	printf("DescribeTask n/a\n");
	if (args[0] == 0x0)
		printf("no args\n");
	return EC_FUTURE_FEATURE;
}
/*!
  Initialize the command mapping
*/
void InitializeMapping()
{
	Mapping = CreateCommandMap(10);
	CMSet(&Mapping, CreateTask, "create-task", "(future) Create a new task.");
	CMSet(&Mapping, SetReminder, "set-reminder", "(future) Set a popup notification.");
	CMSet(&Mapping, DescribeTask, "describe", "(future) Print a description of a task.");
}

/*!
  Text interface entry point
  /return OS return code
*/
int main()
{
	char inputLine[100];
	int nbytes=100;
	int glcode;

	InitializeMapping();
	PrintWelcome();
	while (strcmp(inputLine, "quit") != 0)
	{
		printf(">");
		glcode = getline((char*)&inputLine, &nbytes, stdin);
		if (glcode == -1) {
			printf("IO error: terminating.\n");
			return 1;
		}
		else if (glcode == -2) {
			fflush(stdin);
		}
		else if (strcmp(inputLine, "quit") != 0) {
			if (CMExecute(&Mapping, inputLine) == EC_NOT_FOUND) {
				printf("<no such command>\n");
			}
		}
	}
	return 0;
}
